<template>
  <div class="consultation">
    <h2>订单管理</h2>
    <div class="consultation-block">
      <div class="table" v-loading="tableLoading">
        <el-table size="mini" :data="tableData" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="username" label="订单类型" :formatter="formatType"> </el-table-column>
          <el-table-column prop="createTime" label="订单生成日期" :formatter="formatTime"> </el-table-column>
          <el-table-column prop="name" label="名称" :formatter="formatName"> </el-table-column>
          <el-table-column prop="date" label="生效日期"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="danger"
                v-if="scope.row.username != 'admin'"
                size="mini"
                @click="deleteData(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="currentPage"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getTicket,
  deleteTicket,
} from "../service.js";
import moment from 'moment';
export default {
  name: "home",
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      isVisible: false,
      dialogTitle: "",
      ruleForm: {
        username: "",
        password: "",
        likes: []
      },
      json_fields: {},
      columns: [],
      inputValue: "",
      inputVisible: false,
      isVisible2: false,
      tableDataModal: [],
      columnModel: [],
      title: "",
      roleList: ["普通用户", "会员", "管理员"],
      currentRow: {},
    };
  },
  methods: {
    handleClose() {
      this.isVisible = false;
    },
    handleCurrentChange() {},
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateUser(this.ruleForm._id, {
            password: this.ruleForm.password,
            username: this.ruleForm.username,
            likes: this.ruleForm.likes.join(","),
            type: this.ruleForm.type,
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("编辑成功");
              this.refreshTable();
              this.isVisible = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    closeTag(tag) {
      this.columns.splice(this.columns.indexOf(tag), 1);
      if (this.columns.length) {
        this.ruleForm.column = JSON.stringify(this.columns);
      } else {
        this.ruleForm.column = "";
      }
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.columns.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
      this.ruleForm.column = JSON.stringify(this.columns);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    refreshTable() {
      getTicket().then((res) => {
        this.tableData = res.code === 200 ? res.data : [];
      });
    },
    formatType(row) {
      console.log(row)
      if (row.type === 'hotel') {
        return '酒店订单'
      } else if (row.type === 'train') {
        return '车票'
      } else {
        return '景点订单'
      }
    },
    formatName(row) {
      console.log(row)
      if (row.type === 'hotel') {
        return row.hotelName
      } else {
        return row.funName
      }
    },
    formatTime(row) {
      return moment(row.createTime).format('YYYY-MM-DD HH:mm:ss')
    },
    deleteData(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteTicket(row._id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refreshTable();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
